package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：w
 * @date ：Created in 2019/7/28 0028
 * @description ：角色控制器
 * @version: 1.0
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {
    @Reference
    private RoleService roleService;

    /**
     * 获取角色列表
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Role> roleList = roleService.findAll();
            log.debug("############roleList:{}"+roleList);
            return new Result(true, MessageConst.ACTION_SUCCESS, roleList);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 基于用户ID,获取此用户关联的角色ID
     * @param userId
     * @return
     */@RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer userId) {
         log.debug("##########userId:{}"+userId);
        try {
            List<Integer> roleIds = roleService.findRoleIdsByUserId(userId);
            log.debug("##########roleIds:{}" + roleIds);
            return new Result(true, MessageConst.ACTION_SUCCESS, roleIds);
        } catch (Exception e) {
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
}
